package com.tlias.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.tlias.mapper.EmpMapper;
import com.tlias.pojo.Emp;
import com.tlias.pojo.PageBean;
import com.tlias.pojo.Result;
import com.tlias.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;


@Slf4j
@Service
public class EmpServiceImpl implements EmpService {
    public static final Logger LOGGER = LoggerFactory.getLogger(EmpServiceImpl.class);

    @Autowired
    EmpMapper empMapper;

    //员工分页列表查询
    @Override
    public PageBean page(Integer page, Integer pageSize, String name, Integer gender, LocalDate begin, LocalDate end) {
//        //1、获取总记录数
//        Long count = empMapper.count();
//        //2、获取分页查询结果列表
//        Integer start = (page - 1) * pageSize; //计算起始索引 , 公式: (页码-1)*页大小
//        List<Emp> empList = empMapper.list(start, pageSize);
//        3、封装PageBean对象
//        PageBean pageBean = new PageBean(count, empList);
//        return pageBean;
        log.info("service-page:page[{}],pageSize[{}],name[{}],gender[{}],begin[{}],end[{}]", page, pageSize, name, gender, begin, end);
        //page,pageSize不得为空
        if (page == null || pageSize == null) {
            throw new RuntimeException();
        }
        //PageHelper
        //1.设置分页参数
        PageHelper.startPage(page, pageSize);
        //2.执行查询
        List<Emp> empList = empMapper.list(name, gender, begin, end);
        Page<Emp> p = (Page<Emp>) empList;
        //3.封装PageBean
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }


    /**
     * 新增员工
     *
     * @return 变动行数
     */
    @Override
    public Integer addEmp(Emp emp) {
        if (emp != null) {
            emp.setCreateTime(LocalDateTime.now());
            emp.setUpdateTime(LocalDateTime.now());
            Integer rs = empMapper.insertEmp(emp);
            return rs;
        }


        return null;
    }


    /**
     * 删除员工
     *
     * @param ids
     * @return
     */
    @Override
    public Integer deleteEmp(Integer[] ids) {
        if (ids.length < 1) {
            return 0;
        } else {
            Integer rs = empMapper.delEmps(ids);
            if (rs != null && rs > 0) {
                return 1;
            } else {
                log.error("删除失败");
                return 0;
            }
        }
    }

    /**
     * 修改员工信息
     *
     * @param emp
     * @return
     */
    @Override
    public Integer updateEmp(Emp emp) {
        if (emp.getId() == null) {
            log.error("service-update:id为空");
            return 0;
        }
        emp.setUpdateTime(LocalDateTime.now());
        int rs = empMapper.updateEmpByid(emp);
        return rs;
}

    /**
     * 根据id查员工
     *
     * @param id
     * @return
     */
    @Override
    public Result findEmpById(Integer id) {
        if (id <= 0) {
            return Result.error("非法id");
        } else {
            //查询
            Emp emp = empMapper.selectEmpByPId(id);
            if (emp != null) {  //查询成功
                return Result.success(emp);
            } else {     //不存在
                return Result.error("员工不存在");
            }
        }
    }

}
